﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;

namespace CalendarMgm.asmx
{
    /// <summary>
    /// SelectionBDay 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class SelectionBDay : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public CascadingDropDownNameValue[] getYear(string knownCategoryValues, string category)
        {
            System.Collections.Specialized.StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);    //解析已知的内容
            List<CascadingDropDownNameValue> yearList = new List<CascadingDropDownNameValue>();
            int toyear = DateTime.Today.Year;
            for (int i = toyear; i>=1930;i-- )
            {
                yearList.Add(new CascadingDropDownNameValue(i.ToString(), i.ToString()));
            }
            return yearList.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] getMonth(string knownCategoryValues, string category)
        {
            System.Collections.Specialized.StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);    //解析已知的内容
            List<CascadingDropDownNameValue> monthList = new List<CascadingDropDownNameValue>();
            int tomonth;

            if (kv["year"].Equals(DateTime.Today.Year.ToString()))
            {
                tomonth = DateTime.Today.Month;
            }
            else
            {
                tomonth = 12;
            }

            for (int i = 1; i <= tomonth; i++)
            {
                monthList.Add(new CascadingDropDownNameValue(i.ToString(), i.ToString()));
            }
            return monthList.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] getDay(string knownCategoryValues, string category)
        {
            System.Collections.Specialized.StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);    //解析已知的内容
            List<CascadingDropDownNameValue>dayList = new List<CascadingDropDownNameValue>();
            int today;
            int yearSelected=Convert.ToInt32(kv["year"]);
            int monthSelected=Convert.ToInt32(kv["month"]);

            if (yearSelected==DateTime.Today.Year
                && monthSelected==DateTime.Today.Month)
            {
                today = DateTime.Today.Day;
            }
            else
            {
                today = DateTime.DaysInMonth(yearSelected,monthSelected);
            }

            for (int i = 1; i <= today; i++)
            {
                dayList.Add(new CascadingDropDownNameValue(i.ToString(), i.ToString()));
            }
            return dayList.ToArray();
        }
    }
}
